<template>
  <div>
    <el-row>
      <el-col :span="2" style="padding:5px; ">
        <el-button type="primary" @click="showWin()">新增</el-button>

      </el-col>
      <el-col :span="5" style="padding:5px; ">
        <el-input v-model="keyword" placeholder="请输入内容"></el-input>
      </el-col>
      <el-col :span="17" style="padding:5px; ">
        <el-button type="primary" @click="search()">搜索</el-button>
      </el-col>

    </el-row>

    <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="30%"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <!--                    <input type="text" v-model="form.head">-->
          <el-input v-model="form.username" suffix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item label="用户头像">
          <el-upload
              class="avatar-uploader el-upload1"
              action="http://localhost:8080/users/upload"
              :show-file-list="false"
              :on-success="uploadSuccess"
              :before-upload="beforeUpload"
              with-credentials
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
      </span>
    </el-dialog>

    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column
          prop="uid"
          label="用户编号"
          width="180">
      </el-table-column>
      <el-table-column
          prop="username"
          label="用户名"
      >
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="showWin(scope.row)" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        @current-change="currentChange"
        layout="prev, pager, next"
        :page-size="pageSize"
        :current-page="currentPage"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  created() {
    //界面加载完成往后台发送ajax请求 获取用户数据
    this.loadData();
  },
  name: "UsersVue",
  data() {
    return {
      //总条数,
      total: 100,
      //当前页
      currentPage: 1,
      //每页多少条
      pageSize: 10,
      //搜索的关键字
      keyword: "",
      dialogVisible: false,
      title: "新增用户",
      tableData: [],
      imageUrl: "",
      form: {
        username: ""
      },
      //声明验证规则
      rules: {
        //用户名的校验规则
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    //加载数据
    loadData() {
      //往后台发送ajax请求
      this.$http.post("users/listAll", {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      }).then(res => {
        console.info("后台返回的数据", res);
        this.tableData = res.data.list;
        this.total = res.data.total;
      });
    },
    //弹出窗口
    showWin(row) {
      //如果是编辑
      if (row) {
        this.form = Object.assign({}, row);
        this.title = "编辑用户";
        this.imageUrl = "http://localhost:9999/img/head/" + row.head;
      } else {
        this.title = "新增用户";
        this.form = {
          username: "",
          password: ""
        }
        this.imageUrl = "";
      }
      this.dialogVisible = true;
    },
    //保存或者编辑用户信息
    saveOrUpdate() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          //往后台发送ajax请求
          this.$http.post("users/saveOrUpdate", this.form).then(res => {
            console.info("后台返回的数据", res);
            if (res.data.success) {
              this.loadData();
              this.dialogVisible = false;
            } else {
              this.$message.error(res.data.error);
            }
          });
        } else {
          return false;
        }
      });
    },
    //搜索
    search() {
      this.loadData();
    },
    currentChange(currentPage) {
      this.currentPage = currentPage;
      this.loadData();
    },
    //上传图片成功的回调函数
    uploadSuccess(res) {
      console.info("上传成功后台返回的数据", res);
      this.imageUrl = "http://localhost:9999/img/head/" + res.target;
      //给form的head属性赋值
      this.form.head = res.target;
    },
    //上传之前的回调函数
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}
</script>
<style scoped>
.el-upload1 {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 200px;
}

.el-upload1:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>